$(function () {
    lookMyBalance();
});

//防止ajax重复加载
function prevent_reloading(){
    var pendingRequests = {};
    jQuery.ajaxPrefilter(function( options, originalOptions, jqXHR ) {
        var key = options.url;
        console.log(key);
        if (!pendingRequests[key]) {
            pendingRequests[key] = jqXHR;
        }else{
            //jqXHR.abort();    //放弃后触发的提交
            pendingRequests[key].abort();   // 放弃先触发的提交
        }
        var complete = options.complete;
        options.complete = function(jqXHR, textStatus) {
            pendingRequests[key] = null;
            if (jQuery.isFunction(complete)) {
                complete.apply(this, arguments);
            }
        };
    });
}

var i = 0;
var t;

//获取二维码
function getCode(){
    
    prevent_reloading();

    $("#pay-img").css({"background-image":"url(\""+ctx+"/img/load.gif"+"\")","width":"40px","height":"40px","margin-top": "190px"});
    var userId = $("#userid-text").text();
    var money = $("#num-text").val();
    $.ajax({
        type: 'post',
        url: ctx + "/trade_precreate",   //提交到的url
        cache: false,
        data: {"userId": userId,"money": money},    //提交的参数
        dataType: '',
        success: function (data) {
            if(data.code == 0){
                $("#pay-img").css({"background-image":"url('"+ctx+"/"+data.message+"')","width":"140px","height":"140px","margin-top": "140px"});
                timeGet(data.data);
                i = 0;
            }else{
                $("#pay-img").css({"background-image":"url(\""+ctx+"/img/load.gif"+"\")","width":"40px","height":"40px","margin-top": "190px"});
                i++;
                if(i <= 3){
                    getCode();
                }else{
                    alert("二维码获取失败，重新获取");
                }
            }
        },
        error: function (data) {
            alert("连接失败！");
        }
    });
}

//实时获取充值信息
function timeGet(no){

    prevent_reloading();

    t = setInterval(function(){
        $.ajax({
            type: 'post',
            url: ctx + "/trade_query",   //提交到的url
            cache: false,
            data: {"no": no},    //提交的参数
            dataType: '',
            success: function (data) {
                if(data.code == 0){
                    clearInterval(t);
                    alert("支付成功！");
                    parent.location.reload(true);
                }
            },
            error: function (data) {
                // alert("查询失败！");
            }
        });
    },1000);
}

//充值额度加减
function addMinus(op){
    var t = $(op).text();
    if(t == "-"){
        var num = Number($(op).next().val().trim());
        if(num > 2){
            $(op).next().val(num-1);
            $("#num").text(num-1);
            $("#pay-msg").text("手机支付宝扫码，支付"+(num-1)+"元");
            getCode();
        }else if(num == 2){
            $(op).css("cursor", "not-allowed");
            $(op).next().val(num-1);
            $("#num").text(num-1);
            $("#pay-msg").text("手机支付宝扫码，支付"+(num-1)+"元");
            getCode();
        }else if(num < 2){
            $(op).css("cursor", "not-allowed");
        }
    }else if(t == "+"){
        var num = Number($(op).prev().val().trim());
        $(op).prev().val(num+1);
        $("#num").text(num+1);
        $("#pay-msg").text("手机支付宝扫码，支付"+(num+1)+"元");
        $(op).prev().prev().css("cursor", "pointer");
        getCode();
    }
}

//输入数字鼠标样式的变化
function changeMouse(op){
    var num = Number($(op).val().trim());
    if(num >= 2){
        $(op).prev().css("cursor", "pointer");
    }else{
        $(op).prev().css("cursor", "not-allowed");
    }
    $("#num").text(num);
    $("#pay-msg").text("手机支付宝扫码，支付"+num+"元");
    getCode();
}

//查看我的余额
function lookMyBalance() {
    $("#balance-img").mousedown(function () {
        $("#balance-num").text(balance);
    })
    $("body").mouseup(function () {
        $("#balance-num").text("— —");
    });
}

//跳转奖品中心
function toPrize() {
    //iframe层-禁滚动条
    layer.open({
        type: 2,
        area: ['400px', '180px'],
        skin: 'layui-layer-rim', //加上边框
        content: [ctx+'/prize.jsp', 'no']
    });
}

//充值层显示和隐藏
function toggleBottom(){
    var text = $("#btn-recharge").text();
    if(text == "充值"){
        $("#btn-recharge").text("取消");
        $("#middle-bottom").css("display","block");
        $("#num-text").focus();
        $("#num-text").val("");
        $("#num-text").val("10");
        $("#num").text("10");
        $("#pay-msg").text("手机支付宝扫码，支付10元");
        getCode();
    }else{
        $("#btn-recharge").text("充值");
        $("#middle-bottom").css("display","none");
        clearInterval(t);
    }
}